<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 输入：s = "PAYP ALI SHI RIN G", numRows = 4
      // 输出："PINALSIGYAHRPI"
      // 解释：
      // P     I    N
      // A   L S  I G
      // Y A   H R
      // P     I
      // [[],[],[],[],[]]
      //index指向 字符串  row指向 第几行 也是第几个数组"
      // 要解决的点是要 在循环时往第几个数组中push
      console.log(convert("PAYPALISHIRING", 3));

      function convert(s, r) {
        if (r<2) return s;
        let resString = "";
        const res = [];
        for (i = 0; i < r; i++) res[i] = [];
        let row = 0;
        let len = s.length;
        let flag = -1;
        for (i = 0; i < len; i++) {
          res[row].push(s[i]);
          //这个 开关只有在 row指针等于r活着等于0的时候需要 改变一下
          if (row == 0 || row == r - 1) flag = -flag;
          row += flag;
        }
        console.log(res,"======数组");
        for (item of res) {
          resString += item.join("");
        }
        return resString;
      }
    </script>
  </body>
</html>
